var  app = getApp();
Page({ 
    /**
     * 页面的初始数据
     */
    data: {        
        province:['鲁','京','津','冀','晋','蒙','辽','吉','黑','沪','苏','浙','皖','闽','赣','豫','鄂','湘','粤','桂','琼','渝','川','贵','云','藏','陕','甘','青','宁','新'],
        type:['港','澳','学','警','领'],
        num:['1','2','3','4','5','6','7','8','9','0'],
        letter:['Q','W','E','R','T','Y','U','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'],
        plate:['鲁','','','','','',''],
        number:1,
        save:1
 
    }, 
   onLoad(options) {
      let that=this;
      var status = options.status;
      let id = options.id;
      this.getCarById(id);
      console.log(status);
      if(status == undefined){
          status=-1;
      }
      this.setData({
          currentTab:status 
      })
      var open=wx.getStorageSync('open');
      wx.showShareMenu({
          menus: ['shareAppMessage', 'shareTimeline'],
          success(res) {
            console.log(res)
          },
          fail(e) {
            console.log(e)
          }
        })             
      that.setData({
          platform:app.globalData.platform,    
          open:open,
          id:id,
          width:app.globalData.windowWidth,
          height:app.globalData.windowHeight,
          statusBarHeight:app.globalData.statusBarHeight,
          navHeight:app.globalData.navHeight,
          marginTop:app.globalData.navTitleTop,
          domain:app.globalData.domain,
          uploadUrl:app.globalData.uploadUrl,
        })
  },
    goBack:function(){
      wx.navigateBack();
    },
    showModals:function(msg,url,index){
      wx.showModal({
          title: '系统提示',
          content:msg,
          success:function(res){
            console.log(res)
             if(res.confirm){
              if(url){
                if(index == 'index'){
                  wx.reLaunch({
                    url: url,
                  })
                }else{
                  wx.navigateTo({
                    url: url,
                  })
                }
              }
             }
             if(res.cancel){

             }
          }
      })
  },
    showPro(){
     // var showPro = 1;
      this.setData({
        showPro:1,
        showNum:0,
        save:0
      });
    },
    hidePro(){
      this.setData({
        showPro:0,
        save:1
      });
    },
    setPro(e){
      var value = e.currentTarget.dataset.value;
      var plate = this.data.plate;
      var number = this.data.number
      if(number==0){
        //number=1;
        this.setData({
          number:1
        })
      }
      plate[0] = value;
    
      this.setData({
        plate:plate,
        showPro:0,
        showNum:1

      })
    },
    showNum(){
      let number = this.data.number;
        if(number==0){
          this.setData({
            showPro:1,
            showNum:0,
            save:0
          });
        }else{
          this.setData({
            showPro:0,
            showNum:1,
            save:0
          });
        }
      
     },
     hideNum(){
       this.setData({
         showNum:0,
         save:1
       });
     },
     setNum(e){
      var value = e.currentTarget.dataset.value;
      var plate = this.data.plate;
      var number =this.data.number
      plate[number] = value;
      this.setData({
        plate:plate,
        number:number+1
      })
     },
     delPlate(){
       var number = this.data.number;
       var plate = this.data.plate;
      if(number>=1){
        number=number-1;
        plate[number]='';
         if(number == 0){
          
            this.setData({
              showPro:1,
              showNum:0
            })
          }
         this.setData({
          plate:plate,
          number:number
        })
      }
      
     },
     delPro(){
       var plate = this.data.plate;
       var plates = [];
       console.log(plate)
       plate.forEach(ele=>{
         console.log(ele)
          ele='';
          plates.push('');
       })
       console.log(plate)
      this.setData({
        number:0,
        plate:plates
      })
     },

     /////
     ////
     /////
     setFcous(e){
       var value = e.currentTarget.dataset.id;
       console.log(e);
        this.setData({
          inputId:value
        })
     },
     bindDateChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        date: e.detail.value
      })
    },
    bindMotor(e){
      var value = e.detail.value;
      this.setData({
        motor:value
      })
    },
    bindVin(e){
      var value = e.detail.value;
      this.setData({
        vin:value
      })
    },
    bindKilo(e){
      var value = e.detail.value;
      console.log(e);
      if(value == undefined){
        value=0;
      }
      this.setData({
        kilo:value
      })
    },
    getCarById(id){
      let that = this;
      var open = wx.getStorageSync('open');
      wx.request({
        url:app.globalData.domain + 'wechat/car/getCarById',
        data:{form:'carEdit',open_id:open.id,id:id},
        success:res=>{
          console.log(res);
          var data=res.data;
          if(res.statusCode==200){
            //that.showModals(data.msg,data.url,data.index)
            var plate = data.car.plate;
            var plates = plate.split(',');
            that.setData({
              car:data.car,
              plate:plates,
              kilo:data.car.kilo,
              date:data.car.date,
              vin:data.car.vin,
              motor:data.car.motor

            })
          }else{
            that.showModals('系统错误','#','#')
          }

        }
      })
    },
    saveCar(){
       var open = wx.getStorageSync('open');
       let that = this;
       let data = this.data;
       var number = data.plate.length;
       if(number<7){
         this.showModals('请输入完整车牌号');
         return false
       }else{
         var plate =data.plate.toString();
        wx.request({
          url:app.globalData.domain + 'wechat/car/update',
          data:{form:'carAdd',id:data.id,open_id:open.id,openid:open.openid,plate:plate,kilo:data.kilo,vin:data.vin,motor:data.motor,date:data.date},
          success:res=>{
            console.log(res);
            var data=res.data;
            if(res.statusCode==200){
              that.showModals(data.msg,data.url,data.index)
            }else{
              that.showModals('系统错误','#','#')
            }
          }
        })
       }
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var d = new Date();
     var year = d.getFullYear();
     var month = d.getMonth() + 1;
     if(month<10){
      month = "0"+month;
     }
     var now = year+'-'+month;
     console.log('nowshow')
    
     console.log(now);
     this.setData({
       date:now,
       now:now
     })
  },
    showModals:function(msg,url,index){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
              console.log(res)
               if(res.confirm){
                if(url){
                  if(index == 'index'){
                    wx.reLaunch({
                      url: url,
                    })
                  }else{
                    wx.navigateTo({
                      url: url,
                    })
                  }
                }
               }
               if(res.cancel){

               }
            }
        })
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})